package com.software.backend.login.utils;

/**
 * @author 王俊坤
 * @version 1.0
 * @date 2022/12/12 19:33
 * 用于判断字符串是否为电话号码的类
 */
public class PhoneNumber {

    private static final int PHONE_NUMBER_LENGTH = 11;
    private static final String[] START_NUMBER = {"134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "182", "187", "188", "130", "131", "132", "155", "156", "185", "186", "133", "153", "180", "189", "150", "151", "152", "153", "155", "156", "157", "158", "159", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "180", "182", "185", "186", "187", "188", "189", "154", "181", "183", "184"};

    public static boolean isPhoneNumber(String phoneNumber) {
        if (isAllNumbers(phoneNumber) && phoneNumber.length() == PHONE_NUMBER_LENGTH) {
            String numStart = phoneNumber.substring(0, 3);
            for (String start : START_NUMBER){
                if (numStart.equals(start)){
                    return true;
                }
            }
        }
        return false;
    }

    /**判断字符串是否为全数字*/
    public static boolean isAllNumbers(String str){
        for(int i=str.length();--i >= 0;){
            int chr  =str.charAt(i);
            if(chr<48 || chr>57){
                return false;
            }
        }
        return true;
    }


}
